import * as types from '../mutation-types.js'
import apiurl from '@/api/apiurl'
// import Api from '@/api/index'

//全局状态
const state = {

}

//getters
const getters = {

}

const mutations = {

}

const actions = {
  /**
   * 获取子账号设备列表（和未使用的设备）
   * @param params{ groupId: 群组id }
   * @returns { status: 1, result: { gpsFacilityList: [{  }] }, message: '請求成功' }
   * gpsFacilityList => { facilityId: 主键id, facilityName: 设备名称, facilityState: 使用状态 }
   */
  async fetchListUserFacility({ rootGetters }, params = {}) {
    // console.log(params,'获取子账号设备列表参数')
    // console.log(apiurl.register)
    let groupId = rootGetters['common/groupId']
    let res = await Api.request(apiurl.listUserFacility, {
      groupId: groupId,
      ...params
    })
    let resultList = []
    if(res.result.gpsFacilityList.length) {
      resultList = groupId ? res.result.gpsFacilityList.filter(item => item.groupId == groupId) : res.result.gpsFacilityList
    }
    return resultList
  },

  /**
   * 移除设备
   * @param params{ facilityId: 设备id }
   * @returns { status: 1, result: {}, message: '移除成功' }
   */
  async fetchDelFacility(_, params = {}) {
    // console.log(params,'移除设备参数')
    // console.log(apiurl.delFacility)
    let res = await Api.request(apiurl.delFacility, {
      ...params
    })

    return res
  },

  /**
   * 檢查設備私鑰
   * @param params{ privateKey: 私鑰 }
   *@returns { status: 1, result: { facilityId: 主鍵id }, message: '請求成功' }
   **/
  async fetchCheckFacility(_, params = {}) {
    // console.log(params,'檢查設備私鑰')
    // console.log(apiurl.checkFacility)
    let res = await Api.request(apiurl.checkFacility, {
      privateKey: params.privateKey
    })
    return res
  },

  /**
   * 添加设备
   *
   * @param params{ facilityName: 设备名称, facilityNum: 设备唯一号 id, password: 密碼, facilityId: 主鍵id }
   * @returns { status: 1, result: {}, message: '添加成功' }
   */
  async fetchAddFacility(_, params = {}) {
    // console.log(apiurl.addFacility)
    // console.log(params,'添加设备参数')
    let res = await Api.request(apiurl.addFacility, {
      ...params
    })
    return res
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
